package com.weijianhuawen.wjblog.web.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.weijianhuawen.wjblog.common.domain.dos.ArticleDo;
import com.weijianhuawen.wjblog.common.domain.dos.ArticleTagRelDo;
import com.weijianhuawen.wjblog.common.domain.dos.TagDo;
import com.weijianhuawen.wjblog.common.domain.mapper.ArticleMapper;
import com.weijianhuawen.wjblog.common.domain.mapper.ArticleTagRelMapper;
import com.weijianhuawen.wjblog.common.domain.mapper.TagMapper;
import com.weijianhuawen.wjblog.common.enums.ResponseCodeEnum;
import com.weijianhuawen.wjblog.common.exception.BizException;
import com.weijianhuawen.wjblog.common.utils.PageResponse;
import com.weijianhuawen.wjblog.common.utils.Response;
import com.weijianhuawen.wjblog.web.convert.ArticleConvert;
import com.weijianhuawen.wjblog.web.mode.vo.tag.FindTagArticlePageListReqVo;
import com.weijianhuawen.wjblog.web.mode.vo.tag.FindTagArticlePageListRspVo;
import com.weijianhuawen.wjblog.web.mode.vo.tag.FindTagListReqVo;
import com.weijianhuawen.wjblog.web.mode.vo.tag.FindTagListRspVo;
import com.weijianhuawen.wjblog.web.service.TagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-10-28 23:04
 */

@Service
@Slf4j
public class TagServiceImpl implements TagService {
    @Resource
    private TagMapper tagMapper;
    @Resource
    private ArticleTagRelMapper articleTagRelMapper;
    @Resource
    private ArticleMapper articleMapper;

    @Override
    public Response findTagList(FindTagListReqVo vo) {
        List<TagDo> tagDos = Lists.newArrayList();
        if (Objects.isNull(vo) || Objects.isNull(vo.getSize()) || vo.getSize() <= 0) {
            // 查询所有的tag
            tagDos = tagMapper.selectList(Wrappers.emptyWrapper());
        } else {
            tagDos = tagMapper.selectByLimit(vo.getSize());
        }
        // 将do 转换成 vo
        List<FindTagListRspVo> findTagListRspVos = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(tagDos)) {
            tagDos.forEach(tagDo -> {
                FindTagListRspVo findTagListRspVo = FindTagListRspVo.builder()
                        .id(tagDo.getId())
                        .name(tagDo.getName())
                        .articlesTotal(tagDo.getArticlesTotal())
                        .build();

                findTagListRspVos.add(findTagListRspVo);
            });

        }

        return Response.success(findTagListRspVos);
    }

    @Override
    public PageResponse findTagArticlePageList(FindTagArticlePageListReqVo vo) {
        // 获取标签id 查询页码 一页数据条数
        Long tagId = vo.getId();
        Long current = vo.getCurrent();
        Long size = vo.getSize();

        // 查询标签id 是否存在该标签
        TagDo tagDo = tagMapper.selectById(tagId);

        // 不存在标签，则返回业务异常
        if (Objects.isNull(tagDo)) {
            log.warn("【前台标签服务】标签不存在或已删除");
            throw new BizException(ResponseCodeEnum.NOT_EXIST_TAG);
        }
        // 查询标签文章关联表
        List<ArticleTagRelDo> articleTagRelDos = articleTagRelMapper.selectListByTagId(tagId);

        // 不存在关联文章
        if (CollectionUtils.isEmpty(articleTagRelDos)) {
            // 对应标签id不存在对应关联文章
            log.info("【前台标签服务】该标签：{} 未关联任何文章", tagDo.getName());
            return PageResponse.success(null, null);
        }
        // 获取所有被关联的文章id
        List<Long> articleIds = articleTagRelDos
                .stream()
                .map(ArticleTagRelDo::getArticleId)
                .collect(Collectors.toList());

        // 根据文章id 分页查询文章数据
        Page<ArticleDo> page = articleMapper.selectPageListByArticleIds(current, size, articleIds);
        // 提取数据
        List<ArticleDo> articleDoList = page.getRecords();

        // 将do转vo
        List<FindTagArticlePageListRspVo> vos = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(articleDoList)) {
            vos = articleDoList
                    .stream()
                    .map(articleDo -> ArticleConvert.INSTENCE.convertDoToTagArticleVo(articleDo))
                    .collect(Collectors.toList());
        }

        return PageResponse.success(page, vos);
    }
}
